/**
 * Created by xie on 2018/3/6.
 */
angular.module('module.mainpage')
    .controller('MainPageController', MainPageController);

MainPageController.$inject = ['$scope','$state','$cookieStore','$rootScope','MainpageService'];


function MainPageController($scope, $state,$cookieStore, $rootScope,MainpageService) {
    var vm = this;
    vm.goDetails = goDetails;
    vm.navSpanClass=["spanchecked","span","span","span","span","span","span"];
    vm.getarticlebytype=getarticlebytype;
    vm.getArticleAll=getArticleAll;

    function goDetails(article){
        $state.go("atricle",{articleid:article.articleid});
    }

    function getArticleAll(){
        MainpageService.getArticleAll().$promise.then(function(data){
            vm.articlelist = data;
            console.log(vm.articlelist);
            sort(vm.articlelist);
        },function(err){
            console.log(err)
        });

    }

    function getarticlebytype(type){

        MainpageService.getArticleByType(type).$promise.then(function(data){
            vm.articlelist = data;
            console.log(vm.articlelist);
            sort(vm.articlelist);
        },function(err){
            console.log(err)
        });
    }





    function sort(data){
        vm.left = []
        vm.right = []
        for(var i=0;i<data.length;i++){
            if(i%2==0){
                vm.left.push(data[i]);
            }else{
                vm.right.push(data[i]);
            }
        }

    }



    //
    var sliderImgList = [
        {"action": "", "imgSrc": "images/imggg/slide1.jpg"},
        {"action": "", "imgSrc": "images/imggg/slide2.jpg"},
        {"action": "", "imgSrc": "images/imggg/slide3.jpg"},
        {"action": "", "imgSrc": "images/imggg/slide4.jpg"},
        {"action": "", "imgSrc": "images/imggg/slide5.jpg"},
        {"action": "", "imgSrc": "images/imggg/slide6.jpg"},
        {"action": "", "imgSrc": "images/imggg/slide7.jpg"},
    ];
    var defaults = {
        loop: true,
        smallBtn: true,
        number: false,
        laseMoveFn: true,
        location: false,
        preDef: "lnr",
        autoPlay: true,
        autoplayDisableOnInteraction:true,
        autoHeight: true,
        preFn: null,
        lastImgSlider: false,
        playTime: 2000,
        callback: null,
        fullScreen: true,
        verticalCenter: false,
        total: 0,
        touchRatio:1

    };
    var sliderList = {
        dom: document.getElementById('slider'),
        data: sliderImgList,
        defaults: defaults
    };
    new Slider(sliderList);

    vm.getArticleAll();

    vm.articlefilter = function (num) {
        for(var i=0;i<vm.navSpanClass.length;i++){
            vm.navSpanClass[i]="span";
        }
        vm.navSpanClass[num]="spanchecked";
        switch (num){
            case 0:
                vm.getArticleAll();
                break;

            case 1:
                    vm.getarticlebytype("幽静唯美");
                    break;

            case 2:
                    vm.getarticlebytype("花中萌物");
                    break;

            case 3:
                    vm.getarticlebytype("奇花异草");
                    break;

            case 4:
                    vm.getarticlebytype("网友随拍");
                    break;

            case 5:
                    vm.getarticlebytype("植物与人");
                    break;

            case 6:
                    vm.getarticlebytype("求鉴定");
                    break;


        }
        }





}